package com.xmxsuperstar.app.ttac;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.xmxsuperstar.app.ttc.client.ClientConstants;
import com.xmxsuperstar.app.ttc.client.TwitterStatusesClient;
import com.xmxsuperstar.lib.utils.StringUtils;

/**
 * @author mingxing.xumx
 * @version $Id: Main.java,v 0.1 2011-10-14 下午6:01:37 mingxing.xumx Exp $
 */
public class Main extends TTACActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Log.i(ClientConstants.LogTag, getApnInUse(this));

		setContentView(R.layout.main);

		final EditText et = (EditText) this.findViewById(R.id.status);
		et.setHint("What’s happening?");
		et.clearFocus();

		final Button btn = (Button) this.findViewById(R.id.post);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String status = et.getText().toString();
				if (!StringUtils.isBlank(status)) {
					TwitterStatusesClient tc = new TwitterStatusesClient();
					tc.update(status);
				}

				Intent i = new Intent();
				i.setClass(Main.this, HomeActivity.class);
				Main.this.startActivity(i);
			}
		});

		ListView listView = (ListView) this.findViewById(R.id.main_menu_list);

		listView.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_expandable_list_item_1,
				getMainMenuList()));

		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				if (StringUtils.equalsIgnoreCase(getMainMenuList()
						.get(position), Main.this
						.getString(R.string.time_line_home))) {
					Intent i = new Intent();
					i.setClass(Main.this, HomeActivity.class);
					Main.this.startActivity(i);
				} else if (StringUtils.equalsIgnoreCase(
						getMainMenuList().get(position),
						Main.this.getString(R.string.time_line_retwitted))) {
					Intent i = new Intent();
					i.setClass(Main.this, RetweetedToMeActivity.class);
					Main.this.startActivity(i);
				} else if (StringUtils.equalsIgnoreCase(
						getMainMenuList().get(position),
						Main.this.getString(R.string.time_line_mentions))) {
					Intent i = new Intent();
					i.setClass(Main.this, MentionsActivity.class);
					Main.this.startActivity(i);
				} else if (StringUtils.equalsIgnoreCase(
						getMainMenuList().get(position),
						Main.this.getString(R.string.time_line_profile))) {
					Intent i = new Intent();
					i.setClass(Main.this, ProfileActivity.class);
					Main.this.startActivity(i);
				} else if (StringUtils.equalsIgnoreCase(
						getMainMenuList().get(position),
						Main.this.getString(R.string.time_line_lists))) {
					Intent i = new Intent();
					i.setClass(Main.this, TwitterAllListActivity.class);
					Main.this.startActivity(i);
				}

			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();
		final EditText et = (EditText) this.findViewById(R.id.status);
		et.setText("");
	}

	private List<String> getMainMenuList() {
		List<String> menuList = new ArrayList<String>();
		menuList.add(this.getString(R.string.time_line_home));
		menuList.add(this.getString(R.string.time_line_retwitted));
		menuList.add(this.getString(R.string.time_line_mentions));
		menuList.add(this.getString(R.string.time_line_profile));
		menuList.add(this.getString(R.string.time_line_lists));

		return menuList;
	}

	public String getApnInUse(Context context) {
		String apn = "wifi";

		try {
			ConnectivityManager cm = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo ni = cm.getActiveNetworkInfo();

			String extra = ni.getExtraInfo();
			if (extra == null || extra.indexOf("none") != -1)
				apn = ni.getTypeName();
			else
				apn = extra;
		} catch (Exception e) {
			e.printStackTrace();
		}

		apn = apn.replace("internet", "wifi");
		return apn;
	}

}
